<?php

//------------------------------------------------------------------
// File: players.code.php
// Author: Bryan Frost
// Date Created: 2010-03-25
//
// players.php codebehind
//------------------------------------------------------------------

require_once('dataAccess.php');
require_once('utility.php');
require_once('../classes/Player.class.php');
require_once('../classes/Event.class.php');

$abbreviatedSeason = "'10";
$abbreviatedYear = sprintf('%s%d', "'",date("y"));

$ratingsChartLink = "<a href='ratingsChart.php'>?</a>";

function GetFirstNameSelected()	{ return $_POST['firstName']; }
function GetLastNameSelected()	{ return $_POST['lastName']; }
function GetRatingSelected()	{ return $_POST['ratingsDropDownList']; }
function GetGenderSelected()
{
	if (isset($_POST['gendersDropDownList']))
		return $_POST['gendersDropDownList'];
	else
		return 1;
}

// Create HTML options for ratings (retain selection)
function GetRatingDropDownOptions()
{	
	$array = GetRatings();
	foreach($array as $key => $value)
	{
		$selectedText = ($key == GetRatingSelected() ? " selected=\"selected\"" : "");
		echo "<option value=$key $selectedText>$value</option>";
	}
}

// Create HTML options for gender (retain selection)
function GetGenderDropDownOptions()
{
	$array = GetPlayerGender();
	foreach($array as $key => $value)
	{
		$selectedText = ($key == GetGenderSelected() ? " selected=\"selected\"" : "");
		echo "<option value=$key $selectedText>$value</option>";
	}
}	

// Create HTML table rows for players (based on filters)
function GetPlayerTableRows()
{
	$playerPreviewCollection = Player::GetPlayerPreviews();
	
	$i = 0;
	foreach($playerPreviewCollection as $key => $value)
	{
		$playerId =			$playerPreviewCollection[$key]->GetPlayerId();
		$firstName = 		$playerPreviewCollection[$key]->GetFirstName();
		$lastName = 		$playerPreviewCollection[$key]->GetLastName();
		$name =				$playerPreviewCollection[$key]->GetFullName();
		$ratingName =		$playerPreviewCollection[$key]->GetCurrentRatingName();
		
		if ($ratingName == "Unrated")
			$ratingName = "";
		
		$played =			$playerPreviewCollection[$key]->GetCurrentPlayed();
		$points =			$playerPreviewCollection[$key]->GetCurrentPoints();
		$rating =			$playerPreviewCollection[$key]->GetCurrentRating();
		$genderId =			$playerPreviewCollection[$key]->GetGenderId();

		if ((GetRatingSelected() == 0 || GetRatingSelected() == $rating) &&
			(GetGenderSelected() == 0 || GetGenderSelected() == $genderId) &&
			(GetFirstNameSelected() == NULL || strcasecmp(GetFirstNameSelected(), $firstName) == 0) &&
			(GetLastNameSelected() == NULL || strcasecmp(GetLastNameSelected(), $lastName) == 0))
		{
			$playerLink =	"<a href=playerDetail2.php?player=$playerId>$name</a>";
			
			// Alternate rows have unique css class "alternate"
			$cssRow = ($i & 1 ? "alternate" : "");
			
			echo "<tr class=$cssRow>";
			echo "<td>$playerLink</td>";
			echo "<td>$points</td>";
			echo "<td>$played</td>";
			echo "<td class=last>$ratingName</td>";
			echo "</tr>";
			
			$i++;
		}
	}
}
	
?>